package com.console.operations.planmgr;

import java.math.BigDecimal;

import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.console.pages.planmgr.FinancePlanDepositPage;
import com.console.util.FinancePlanDepositUtil;

	/**
		@author 王嘉犇
		@name 理财计划后台投资
		@type 动作类
	 */

public class JbpFinancePlanDeposit extends OperationBase {
	
	public JbpFinancePlanDeposit(UITestBase tb){
		super(tb);
	}

	public void deposit() throws Exception{
		switchToDefaultContent();
		FinancePlanDepositPage fpdp = new FinancePlanDepositPage(tb);
		waitForElementExist(fpdp.panelheaddiv);
		waitForElementDisplayed(fpdp.panelheaddiv);
		BigDecimal depositfeebd = new BigDecimal(FinancePlanDepositUtil.DEPOSITFEE).setScale(2, BigDecimal.ROUND_DOWN);
		log.info("代投金额为 " + depositfeebd);
		waitForElementExist(fpdp.thispagelastdatasurplustd);
		//得到理财计划代投页面最好一条数据的可投金额
		BigDecimal nbsurplusbeforedepositebd = new BigDecimal(fpdp.thispagelastdatasurplustd.getText().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_DOWN);
		log.info("理财计划剩余可代投金额为 " + nbsurplusbeforedepositebd);
		String period = fpdp.periodtd.getText();
		log.info("周期为 " + period);
		BigDecimal leftusercashaccountbd = new BigDecimal(DBUtil.queryOneCell("select nb_balance\n"
																			+ "from fiz_user_account\n"
																			+ "where fk_user_id = (\n"
																			+ "                    select pk_id\n"
																			+ "                    from fiz_user\n"
																			+ "                    where vc_cellphone = ?\n"
																			+ "                   )"
																			+ "  and dc_type = ?", new Object[]{FinancePlanDepositUtil.MOBILE, "00"})).setScale(2, BigDecimal.ROUND_DOWN);
		log.info("投资用户现金账户余额为 " + leftusercashaccountbd);
		fpdp.thispagelastdatainvestLink.click();
		waitForElementDisplayed(fpdp.cellphoneinput);
		fpdp.cellphoneinput.type(FinancePlanDepositUtil.MOBILE);
		pause(700);
		fpdp.searchButton.click();
		waitForPageContainsStr("账户余额：");
		pause(1000);
		if(nbsurplusbeforedepositebd.compareTo(new BigDecimal(0).setScale(2, BigDecimal.ROUND_DOWN))==0){
			log.info("理财计划剩余可代投金额为0，投资金额输入框将会失效");
			waitForElementDisabled(fpdp.amountinput);
		}else{
			log.info("理财计划剩余可代投金额不为0");
			if(leftusercashaccountbd.compareTo(new BigDecimal(0).setScale(2, BigDecimal.ROUND_DOWN))==0){
				log.info("投资用户现金账户余额为0，投资金额输入框将会失效");
				waitForElementDisabled(fpdp.amountinput);
			}else{
				log.info("投资用户现金账户余额不为0");
				fpdp.amountinput.type(FinancePlanDepositUtil.DEPOSITFEE);
				pause(1000);
				if(leftusercashaccountbd.compareTo(depositfeebd)==-1){
					log.info("投资用户现金账户余额不为0但是小于代投金额，确定投资按钮将会失效");
					waitForElementDisabled(fpdp.assuredepositButton);
				}else{
					log.info("投资用户现金账户余额大于代投金额");
					if(nbsurplusbeforedepositebd.compareTo(depositfeebd)==-1){
						log.info("代投金额大于理财计划剩余可购买金额，确定投资按钮将会失效");
						waitForElementDisabled(fpdp.assuredepositButton);
					}else{
						log.info("代投金额小于理财计划剩余可购买金额");
						if(period.contains("周")){
							log.info("该理财计划为周周升计划");
							if(DBUtil.isQueryData("select *\n"
												+ "from fiz_user_prop\n"
												+ "where fk_user_id = (\n"
												+ "					   select pk_id\n"
												+ "					   from fiz_user\n"
												+ "					   where vc_cellphone = ?\n"
												+ "					  )\n"
												+ "  and vc_code = 'weekly-limit'", new Object[]{FinancePlanDepositUtil.MOBILE})){
								log.info("用户设置了周周升计划额度");
								String weeklylimit = DBUtil.queryOneCell("select vc_value\n"
																	   + "from fiz_user_prop\n"
																	   + "where fk_user_id = (\n"
																	   + "					  select pk_id\n"
																	   + "					  from fiz_user\n"
																	   + "					  where vc_cellphone = ?\n"
																	   + "					 )\n"
																	   + "  and vc_code = 'weekly-limit'", new Object[]{FinancePlanDepositUtil.MOBILE});
								BigDecimal weeklylimitbd = new BigDecimal(weeklylimit).setScale(2, BigDecimal.ROUND_DOWN);
								log.info("周周升计划额度为 " + weeklylimitbd);
								String weeklylimitused = DBUtil.queryOneCell("select vc_value\n"
										   								   + "from fiz_user_prop\n"
										   								   + "where fk_user_id = (\n"
										   								   + "					  select pk_id\n"
										   								   + "					  from fiz_user\n"
										   								   + "					  where vc_cellphone = ?\n"
										   								   + "					 )\n"
										   								   + "  and vc_code = 'weekly-limit-used'", new Object[]{FinancePlanDepositUtil.MOBILE});
								BigDecimal weeklylimitusedbd = new BigDecimal(weeklylimitused).setScale(2, BigDecimal.ROUND_DOWN);
								log.info("周周升计划已用额度为 " + weeklylimitusedbd);
								BigDecimal weeklylimitsurplusbd = weeklylimitbd.subtract(weeklylimitusedbd);
								log.info("周周升计划剩余可用额度为 " + weeklylimitsurplusbd);
								if(depositfeebd.compareTo(weeklylimitsurplusbd)==1){
									log.info("投资金额大于用户设置周周升计划剩余可用额度");
									fpdp.assuredepositButton.click();
									waitForPageContainsStr("可投资金额度不足");
								}else{
									log.info("投资金额小于或等于用户设置周周升计划剩余可用额度");
									fpdp.assuredepositButton.click();
									waitForPageContainsStr("投资成功");
								}
							}else{
								log.info("用户没有设置周周升计划额度");
								fpdp.assuredepositButton.click();
								waitForPageContainsStr("可投资金额度不足");
							}
						}else{
							log.info("该理财计划不为周周升计划");
							fpdp.assuredepositButton.click();
							waitForPageContainsStr("投资成功");
						}
					}
				}
			}
		}
		pause(1000);
	}
}
